//
//  Swift-headFile.swift
//  BaibuSeller
//
//  Created by 符艺柏 on 2019/12/17.
//  Copyright © 2019 whawhawhat. All rights reserved.
//

import UIKit
import SnapKit
import HandyJSON
import Alamofire


let KScreenWidth: CGFloat = UIScreen.main.bounds.size.width
let KScreenHeight: CGFloat = UIScreen.main.bounds.size.height
let SCREEN_WIDTH = UIScreen.main.bounds.width;  // swiftlint:disable:this identifier_name
let SCREEN_HEIGHT = UIScreen.main.bounds.height;  // swiftlint:disable:this identifier_name
let KScreenScalewidth = UIScreen.main.bounds.width/320

let KIsIphoneX: Bool = KIsIphoneXTest()
let KIPHONE_NavigationBar_Height:CGFloat = KIsIphoneX ? 88 : 64  // swiftlint:disable:this identifier_name
let KIPHONE_StatusBar_Height:CGFloat = KIsIphoneX ? 44 : 20  // swiftlint:disable:this identifier_name
let KIPHONE_SAFEBOTTOMAREA_Height:CGFloat = KIsIphoneX ? 34 : 0  // swiftlint:disable:this identifier_name
let KIPHONE_ItemBar:CGFloat = KIsIphoneX ? 83 : 49 // swiftlint:disable:this identifier_name
let appDelegate = (UIApplication.shared.delegate) as? AppDelegate

func KIsIphoneXTest() -> Bool {
    if #available(iOS 11, *) {
       //高于 iOS11
        if let height: CGFloat = appDelegate?.window?.safeAreaInsets.bottom {
           if height > 0.0 {
            return true;
           }
       }
    }
    return false;
}

enum JYChangeInfo {
    case changeInfoName // 修改名字
    case changeInfoCompanyName // 修改公司名字
    case changeInfoCompanyTel // 修改公司座机电话
}

func viewRadius(_ view: UIView, _ radius: CGFloat) {
    view.layer.cornerRadius = radius
    view.clipsToBounds = true
}

func viewRadiusWithBorderWidthForColor(_ view: UIView, _ radius: CGFloat, _ borderWidth: CGFloat,_ borderColor: UIColor) {
    view.layer.cornerRadius = radius;
    view.layer.masksToBounds = true;
    view.layer.borderWidth = borderWidth;
    view.layer.borderColor = borderColor.cgColor
}

func getNULLStringSwift(_ obj: Any?) -> String {
    
    if obj == nil {
        return "";
    }
    if obj is NSNull {
       return "";
    }
    if obj is String {
        if let objStr = obj as? String {
            return objStr
        } else {
            return ""
        }
    }
    if obj is NSNumber {
        if let objClass = obj as? NSNumber {
            return objClass.stringValue
        } else {
            return ""
        }
    }
    return ""
}

//计算文字size
func calculateTextContentSize(_ contentText:NSString, font:UIFont) -> CGSize {
    
    let attributeString = [NSAttributedString.Key.font:font]
    
    let drawSize = CGSize.init(width: LONG_MAX, height: LONG_MAX);
    let rect = contentText.boundingRect(with: drawSize, options: .usesLineFragmentOrigin, attributes: attributeString, context: nil)
    
    return rect.size
}

let BSAPIsPhone = "4000680040";


extension UIView {
    static func loadFromNib() -> UIView {
        if let view = Bundle.main.loadNibNamed(NSStringFromClass(self), owner: nil, options: nil)?.first as? UIView {
            return view
        }
        return UIView()
    }
}


//MARK:颜色相关
func RGB(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat = 1) -> UIColor {
    return UIColor.init(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: alpha)
}

/// 设置颜色与透明度 示例：UIColorHEX_Alpha(0x26A7E8, 0.5)
func UIColorHexAlpha(value:UInt32, alpha:CGFloat = 1) -> UIColor {
    let color = UIColor.init(red: (((CGFloat)((value & 0xFF0000) >> 16)) / 255.0), green: (((CGFloat)((value & 0x00FF00) >> 8)) / 255.0), blue: (((CGFloat)((value & 0x0000FF))) / 255.0), alpha: alpha)
    return color
}

//MARK:数组相关
extension Array {
    // 数组中模型去重 非模型: array.filterDuplicates({$0}) 模型:array.filterDuplicates({$0.ID})
    func filterDuplicates<E: Equatable>(_ filter: (Element) -> E) -> [Element] {
        var result = [Element]()
        for value in self {
            let key = filter(value)
            if !result.map({filter($0)}).contains(key) {
                result.append(value)
            }
        }
        return result
    }
}
